/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import utils.nucleo.Nucleo;

/**
 *
 * @author Oliver Kraemer
 */
public class Principal extends javax.swing.JFrame {

    Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
    
    /**
     * Creates new form Principal
     */
    public Principal() {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setTitle("Burn-Down Manager");
    }

    public void abrirTela(Class classe) {
        abrirTela(classe, true);
    }
    
    public void abrirTela(Class classe, boolean maximizado) {
        try {
            abrirTela((JPanel) classe.newInstance(), maximizado);
        } catch (InstantiationException | IllegalAccessException ex) {
            JOptionPane.showConfirmDialog(null, ex.getMessage());
        }
    }
    
    public void abrirTela(JPanel panel) {
        abrirTela(panel, true);
    }

    public void abrirTela(JPanel panel, boolean maximizado) {
        for (JInternalFrame jif : jDesktop.getAllFrames()) {
            for (int i = 0; i <= jif.getContentPane().getComponentCount() - 1; i++) {
                if (jif.getContentPane().getComponent(i).getClass().getName().equals(panel.getClass().getName())) {
                    return;
                }
            }
        }

        if (panel == null) {
            return;
        }

        JInternalFrame frame = new JInternalFrame(
                panel.getToolTipText(), false, true, true, true);
        
        //anexa painel ao painel de conteudo da frame interna 
        Container container = frame.getContentPane();
        container.add(panel, BorderLayout.CENTER);

        BasicInternalFrameUI ui = (BasicInternalFrameUI) frame.getUI();
        Container north = (Container) ui.getNorthPane();
        north.remove(0);
        north.validate();
        north.repaint();

        //configura o tamanho da frame interna com o tamenho de seu conteudo 

        frame.pack();

        //anexa a frame interna a area de trabalho e a exibe 
        jDesktop.add(frame);
        frame.setVisible(true);
        
        int ldesk = jDesktop.getWidth();
        int adesk = jDesktop.getHeight();
        int lframe = frame.getWidth();
        int aframe = frame.getHeight();
        
        frame.setLocation(ldesk/2 - lframe/2, adesk /2 - aframe/2);

        try {
            frame.setMaximum(maximizado);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public void fecharTela() {
        fecharTela(null);
    }
    
    public void fecharTela(JPanel panel) {
        for (JInternalFrame jif : jDesktop.getAllFrames()) {
            for (int i = 0; i <= jif.getContentPane().getComponentCount() - 1; i++) {
                if ( panel == null ) {
                    jif.dispose();
                } else if (jif.getContentPane().getComponent(i).getClass().getName().equals(panel.getClass().getName())) {
                    jif.dispose();
                    return;
                }
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktop = new javax.swing.JDesktopPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMProjetos = new javax.swing.JMenu();
        jMCadastros = new javax.swing.JMenu();
        jMCadastroProjeto = new javax.swing.JMenuItem();
        jMCadastroPessoa = new javax.swing.JMenuItem();
        jMCadastroCliente = new javax.swing.JMenuItem();
        jMSistema = new javax.swing.JMenu();
        Deslogar = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(100, 100, 0, 0));

        jMProjetos.setText("Projetos");
        jMenuBar1.add(jMProjetos);

        jMCadastros.setText("Cadastros");

        jMCadastroProjeto.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        jMCadastroProjeto.setText("Projeto");
        jMCadastroProjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMCadastroProjetoActionPerformed(evt);
            }
        });
        jMCadastros.add(jMCadastroProjeto);

        jMCadastroPessoa.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        jMCadastroPessoa.setText("Pessoa");
        jMCadastroPessoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMCadastroPessoaActionPerformed(evt);
            }
        });
        jMCadastros.add(jMCadastroPessoa);

        jMCadastroCliente.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMCadastroCliente.setText("Cliente");
        jMCadastroCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMCadastroClienteActionPerformed(evt);
            }
        });
        jMCadastros.add(jMCadastroCliente);

        jMenuBar1.add(jMCadastros);

        jMSistema.setText("Sistema");
        jMSistema.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                jMSistemaMenuSelected(evt);
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }
        });

        Deslogar.setText("Deslogar");
        Deslogar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeslogarActionPerformed(evt);
            }
        });
        jMSistema.add(Deslogar);

        jMenuItem1.setText("Sair");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMSistema.add(jMenuItem1);

        jMenuBar1.add(jMSistema);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 837, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jMCadastroPessoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMCadastroPessoaActionPerformed
        abrirTela(GridPessoas.class);
    }//GEN-LAST:event_jMCadastroPessoaActionPerformed

    private void jMSistemaMenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_jMSistemaMenuSelected
    }//GEN-LAST:event_jMSistemaMenuSelected

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMCadastroProjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMCadastroProjetoActionPerformed
        abrirTela(GridProjetos.class);
    }//GEN-LAST:event_jMCadastroProjetoActionPerformed

    private void jMCadastroClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMCadastroClienteActionPerformed
        abrirTela(GridCliente.class);
    }//GEN-LAST:event_jMCadastroClienteActionPerformed

    private void DeslogarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeslogarActionPerformed
        Nucleo.getInstance().setUser(null);
    }//GEN-LAST:event_DeslogarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        Nucleo.getInstance();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Deslogar;
    private javax.swing.JDesktopPane jDesktop;
    private javax.swing.JMenuItem jMCadastroCliente;
    private javax.swing.JMenuItem jMCadastroPessoa;
    private javax.swing.JMenuItem jMCadastroProjeto;
    private javax.swing.JMenu jMCadastros;
    private javax.swing.JMenu jMProjetos;
    private javax.swing.JMenu jMSistema;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    // End of variables declaration//GEN-END:variables
}
